/*
 * Copyright (c) 2010, University of Innsbruck, Austria.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package at.sti2.sepn.pattern.network.node;

import java.util.ArrayList;
import java.util.List;

import at.sti2.sepn.pattern.network.WorkingMemoryElement;
import at.sti2.sepn.pattern.network.beta.BetaMemory;
import at.sti2.sepn.pattern.network.beta.NegativeJoinResult;
import at.sti2.sepn.pattern.network.beta.ProductionNode;

public class Token {

	private Token parent = null;
	private WorkingMemoryElement wme = null;

	// Added for retracting purposes
	private RETENode node = null; // Points to the memory where this token is in
	private List<Token> children = null;
	
	//Added for negated conditions
	private List <NegativeJoinResult> joinResults = null;

	public Token() {
		children = new ArrayList <Token>();
		joinResults = new ArrayList <NegativeJoinResult> ();
	}

	public Token(Token parent, WorkingMemoryElement wme, RETENode node) {
		this.parent = parent;
		this.wme = wme;
		this.node = node;
		children = new ArrayList<Token>();
	}

	public Token getParent() {
		return parent;
	}

	public void setParent(Token parent) {
		this.parent = parent;
	}

	public WorkingMemoryElement getWme() {
		return wme;
	}

	public void setWme(WorkingMemoryElement wme) {
		this.wme = wme;
	}

	public RETENode getNode() {
		return node;
	}

	public void setNode(RETENode node) {
		this.node = node;
	}

	public List<Token> getChildren() {
		return children;
	}

	public void addChild(Token child) {
		children.add(child);
	}
	
	public void addNegativeJoinResult(NegativeJoinResult negativeJoinResult){
		joinResults.add(negativeJoinResult);
	}

	public List<NegativeJoinResult> getNegativeJoinResults() {
		return joinResults;
	}

	public void deleteTokenAndDescendents() {
	
		for (Token childToken : children)
			childToken.deleteTokenAndDescendents();
		
		//Remove token from the list of node items
		//TODO Beta and production node are basically the same so it should inherit the same parent
		if (node instanceof BetaMemory)
			((BetaMemory)node).getItems().remove(this);
		else if (node instanceof ProductionNode)
			((ProductionNode)node).getItems().remove(this);
			
		
		//Remove token from the list of tokens in WME
		wme.getTokens().remove(this);
		
		//Remove token from the list of parent children
		parent.getChildren().remove(this);
		
	}
}
